export const findObjFromArray = (key, keyVal, childName, arr) => {
  if (!Array.isArray(arr)) {
    throw new Error("function findObjFromArray forth parameter must be Array");
  }
  let obj = -1;
  for (let i = 0; i < arr.length; ++i) {
    if (Object.prototype.hasOwnProperty.call(arr[i], key)) {
      if (arr[i][key] === keyVal) {
        obj = arr[i];
        break;
      } else if (arr[i][childName]) {
        //递归
        let childObj = findObjFromArray(
          key,
          keyVal,
          childName,
          arr[i][childName]
        );
        if (childObj !== -1) {
          obj = childObj;
          break;
        }
      }
    } else if (Object.prototype.hasOwnProperty.call(arr[i], childName)) {
      //递归
      let childObj = findObjFromArray(
        key,
        keyVal,
        childName,
        arr[i][childName]
      );
      if (childObj !== -1) {
        obj = childObj;
        break;
      }
    }
  }

  return obj;
};
